package Date;

import org.junit.Test;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAccessor;

/**
 * @Author wangzhipeng
 * @Date 2020-08-11 2:06 下午
 * @Description
 */
public class LocalDateDemo {
    @Test
    public void test01(){
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);//2020-08-11

        //of
        LocalDate localDate1 = LocalDate.of(2020,8,1);
        System.out.println(localDate1);//2020-08-01

        //parse 字符串转换
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate2 = LocalDate.parse("2020-08-11",dateTimeFormatter);
        System.out.println(localDate);//2020-08-11
    }

    @Test
    public void test02(){
        LocalDate localDate = LocalDate.now();
        System.out.println("获取年："+localDate.get(ChronoField.YEAR)); // 也可以 localDate.getYear();
        System.out.println("获取月："+localDate.get(ChronoField.MONTH_OF_YEAR));
        System.out.println("获取日："+localDate.get(ChronoField.DAY_OF_MONTH));
    }

    @Test
    public void test03(){
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);
        //依据单位进行调整
        LocalDate localDate1 = localDate.plus(5, ChronoUnit.DAYS); //localDate.plusDays()也可以
        System.out.println(localDate1);
        //localDate.minus() 减，与加用法一样
    }

    //求两天时间的间隔
    @Test
    public void test05(){
        LocalDate localDate = LocalDate.now();
        LocalDate localDate1 = LocalDate.of(2020,7,22);
        long days = localDate1.until(localDate,ChronoUnit.DAYS);
        System.out.println("两天时间的间隔"+days);
    }

    //with
    @Test
    public void test06(){
        LocalDate localDate = LocalDate.now();//2020-08-11
        //直接调整字段
        LocalDate localDate2 = localDate.with(ChronoField.MONTH_OF_YEAR,5);
        System.out.println(localDate2);//2020-05-11
    }

    //日期比较
    @Test
    public void test07(){
        LocalDate localDate = LocalDate.now();
        LocalDate localDate1 = localDate.plus(5,ChronoUnit.DAYS);

        //判断两个日期是否相等
        localDate.equals(localDate1);
        //之前
        localDate.isBefore(localDate1);
        //之后
        localDate.isAfter(localDate1);


    }

    @Test
    public void test08(){
        //格式化
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        LocalDate localDate = LocalDate.now();
        System.out.println(dateTimeFormatter.format(localDate));//2020年08月11日

        // String s = "2020年06月15日";
        // TemporalAccessor parse = dateTimeFormatter.parse(s);
        // System.out.println(parse);

        //字符串转LocalDate
        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        LocalDate localDate2 = LocalDate.parse("2020年08月11日",dateTimeFormatter1);
        System.out.println(localDate);//2020-08-11

    }

    //用法和上述一样---时分秒
    @Test
    public void localTime(){
        LocalTime localTime = LocalTime.now();
        System.out.println(localTime);//15:18:36.426
        LocalTime localTime1 = LocalTime.of(11,23,59);
        System.out.println(localTime1);//11:23:59
    }





}
